package com.wujian.love.study.sufa;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: Demo
 * @Description: 算法
 * @Author: wuj
 * @Date: 2021-02-24 16:40
 **/
public class Demo {

    //二维数组
    //int[][] arr = new int[3][5];---定义了一个整型的二维数组，其中包含3个一维数组，每个一维数组可以存储5个整数


    public static void main(String[] args) {
        //String [] words = new String{"apple","pleas","please"};
//["aelwxyz","aelpxyz","aelpsxy","saelpxy","xaelpsy"]

        //findNumOfValidWords();

    }
    /**
     * 给定一个二进制矩阵 A，我们想先水平翻转图像，然后反转图像并返回结果。
     * <p>
     * 水平翻转图片就是将图片的每一行都进行翻转，即逆序。例如，水平翻转 [1, 1, 0] 的结果是 [0, 1, 1]。
     * <p>
     * 反转图片的意思是图片中的 0 全部被 1 替换， 1 全部被 0 替换。例如，反转 [0, 1, 1] 的结果是 [1, 0, 0]。
     * <p>
     * 输入：[[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]
     * 输出：[[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
     * 解释：首先翻转每一行: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]]；
     * 然后反转图片: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
     */

    public int[][] flipAndInvertImage(int[][] A) {
        int[][] res = new int[A.length][A[0].length];
        int arraysLen = A.length;
        int arrayLen = A[0].length;
        for (int i = 0; i < arraysLen; i++) {
            int index = 0;
            for (int j = arrayLen - 1; j >= 0; j--) {
                res[i][index] = A[i][j] ^ 1;
                index++;
            }
        }
        return res;

    }

    public int[][] flipAndInvertImage_one(int[][] A) {
        int n = A.length;
        for (int i = 0; i < n; i++) {
            int left = 0, right = n - 1;
            while (left < right) {
                if (A[i][left] == A[i][right]) {
                    A[i][left] ^= 1;
                    A[i][right] ^= 1;
                }
                left++;
                right--;
            }
            if (left == right) {
                A[i][left] ^= 1;
            }
        }
        return A;
    }

    /**
     * 给你一个二维整数数组 matrix， 返回 matrix 的 转置矩阵 。矩阵的 转置 是指将矩阵的主对角线翻转，交换矩阵的行索引与列索引。
     * 输入：matrix = [[1,2,3],[4,5,6],[7,8,9]]
     * 输出：[[1,4,7],[2,5,8],[3,6,9]]
     *
     * 输入：matrix = [[1,2,3],[4,5,6]]
     * 输出：[[1,4],[2,5],[3,6]]
     */

    public int[][] transpose(int[][] matrix) {
        int a = matrix.length;
        int b = matrix[0].length;
        int [][] arr = new int[b][a];
        for (int i = 0;i < a;i++){  //[1,2,3]
            for(int j = 0;j < b;j++){ //
                arr[i][j] = matrix[j][i];
            }

        }
        return arr;

    }

    /**
     * 输入：
     * words = ["aaaa","asas","able","ability","actt","actor","access"],
     * puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"]
     * 输出：[1,1,3,2,4,0]
     *
     * ["apple","pleas","please"]
     * ["aelwxyz","aelpxyz","aelpsxy","saelpxy","xaelpsy"]
     *
     */
    public List<Integer> findNumOfValidWords(String[] words, String[] puzzles) {
        List<Integer> list = new ArrayList<>();
        for (String puzzle : puzzles) {
            int number = 0;
            //aboveyz
            char[] char1 = puzzle.toCharArray();
            char c = char1[0];
            for (String word:words){
                boolean flag = true;
                if(word.indexOf(c) == -1){
                    break;
                }else{
                    char[] charWords = word.toCharArray();
                    for(char a:charWords){
                        if(puzzle.indexOf(a) == -1){
                            flag = false;
                            break;
                        }

                    }
                    if(flag){
                        number++;
                    }
                }
            }
            System.out.println(number);
            list.add(number);


        }

        return list;

    }


}
